package cn.openea.perpartner.meily.web.mapper;

import cn.openea.perpartner.meily.constant.enums.GoodsTypeEnums;
import cn.openea.perpartner.meily.web.entity.Goods;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;

/**
 * (Goods)表数据库访问层
 *
 * @author Weird077
 * @since 2023-01-11 21:04:18
 */
public interface GoodsMapper extends BaseMapper<Goods> {

    /**
     * 统计商品每种类型的数量
     */
    @Select("SELECT COUNT(CASE WHEN goods_type = 'SIGNED' THEN 1 END)               AS num_signed,\n" +
            "       COUNT(CASE WHEN goods_type = 'NINE_POINT_NINE_ZONE' THEN 1 END) AS num_nine_point_nine_zone,\n" +
            "       COUNT(CASE WHEN status = '已上架' THEN 1 END)                      AS num_shelf,\n" +
            "       COUNT(CASE WHEN status = '已下架' or status = '' THEN 1 END)       AS num_off_shelf\n" +
            "FROM meily_goods ${ew.customSqlSegment}")
    Map<String, Object> countGoodsTypeNum(@Param("ew") LambdaQueryWrapper<Goods> buildGoodsWrapper);


    @Update("UPDATE meily_goods SET goods_top = #{goodsTop} ,update_user = #{userId} WHERE goods_id =#{goodsId}")
    int topGoods(@Param("goodsId") Long goodsId,@Param("goodsTop") String goodsTop,@Param("userId") String userId);

    @Update("UPDATE meily_goods SET goods_top = '' ,update_user = #{userId} WHERE goods_id =#{goodsId}")
    int cancelTopGoods(@Param("goodsId") Long goodsId,@Param("userId") String userId);

    Map<String, Integer> statisticTypeQuantity(@Param("typeArray") GoodsTypeEnums[] values,@Param("ew") LambdaQueryWrapper<Goods> buildGoodsWrapper);

    Map<String, Integer> statisticStatusQuantity(@Param("ew") LambdaQueryWrapper<Goods> buildGoodsWrapper);

    /**
     * 计算指定企业关联商品数量
     *
     * @param enterpriseNo 企业编号
     * @return Integer
     */
    @Select("SELECT count(1) FROM meily_goods WHERE enterprise_no = #{enterpriseNo}")
    Integer countByEnterpriseNo(@Param("enterpriseNo") String enterpriseNo);

    @Update("UPDATE meily_goods SET enterprise_no = #{enterpriseNo} , enterprise_name = #{enterpriseName} WHERE goods_id = #{goodsId}")
    void relevancyEnterprise(@Param("goodsId") Long goodsId,@Param("enterpriseNo") String enterpriseNo,@Param("enterpriseName") String enterpriseName);
}

